const http = 'https://m.dcdapp.com'
const axios = require('axios')
let data

const options = ['外观', '内饰']

class dataController {
    // done
    async carData(ctx) {
        await axios.get(`${http}/motor/brand/m/v1/brand/`).then(_res => {
            data = _res.data
        })
        return data
    }
    // done but 需要 存储数据库
    async homeData(ctx) {
        await axios.get(`${http}/motor/stream_entrance/get_feed/v47/?aid=1230&category=motor_car&web_id=698956495569605583${Math.floor(Math.random() * 10)}`).then(_res => {
            data = _res
            // todo 存数据库
            try {
                data = data.data.data.map((p) => {
                    return {
                        only_id: p.unique_id_str,
                        info: {
                            name: p.info.user_info.name,
                            behot_time: p.info.behot_time,
                            display_time: p.info.display_time,
                            image: p.info.image_list[0].url,
                            title: p.info.title
                        }
                    }
                })
            } catch (err) {
                data = data.data
            }
            console.log('homeData');
        })
        return data
    }
     // done but 需要 存储数据库
    async getCarsPicture(ctx) {
        // todo
        console.log(ctx.request.url.split('=')[1]);
        await axios.get(`${http}/motor/car_page/m/v3/get_picture_head_v2/?series_id=${ctx.request.url.split('=')[1]}`).then(_res => {
            data = _res
            data = {
                series_entrance_info: _res.data.data.series_entrance_info,
                category_list: _res.data.data.category_list.filter((p) => {
                    if(options.includes(p.text)) return p
                })
            }
            console.log('getCarspicture');
        })
        return data
    }
     // done
    async selectDetail(ctx) {
        await axios.get(`${http}/motor/brand/m/v5/brand_series_list/?brand_id=${ctx.request.url.split('=')[1]}`).then(_res => {
            _res = _res.data.data
            const online_list = _res.online_list.map((p) => {
                return {
                    category_name: p.category_name,
                    series_list: p.series_list.map((p) => {
                        const flag = p.type === '1003' ? true : false
                        return flag ? {
                            flag,
                            sub_brand_name: p.info.sub_brand_name
                        } : {
                            flag,
                            info: {
                                series_id:p.info.series_id,
                                image_url:p.info.image_url,
                                series_name:p.info.series_name,
                                outter_name:p.info.dcd_score.outter_name,
                                official_price:p.info.official_price,
                                year: p.info.year
                            }
                        }
                    })
                }
            })
            data = {
                brand_name: _res.brand_name,
                online_list: online_list
            }
            console.log('selectDetail');
        })
        return data
    }
    // todo
    async searchCar(ctx) {
        const CarName = ctx.request.url.split('=')[1]
        await axios.get(`${http}/motor/search/api/2/wap/search_content/?keyword=${encodeURI(CarName)}&format=json&cur_tab=6&count=10`).then(_res => {
            data = _res
            console.log('searchCar');
        })
        return data.data
    }
     // done
    async concernDetail(ctx) {
        await axios.get(`${http}/motor/m/article/detail?group_id=${ctx.request.url.split('=')[1]}`).then(_res => {
            data = {
                name: _res.data.data.source,
                content: _res.data.data.content,
                title: _res.data.data.title,
                fans: _res.data.data.comment_count,
                picture: _res.data.data?.media_user?.avatar_url
            }
            console.log('concernDetail');
        })
        return data
    }
     // done
    async carDetail(ctx) {
        await axios.get(`${http}/motor/car_page/m/v1/get_head/?series_id=${ctx.request.url.split('=')[1]}`).then(_res => {
            data = _res.data
            data = {
                baseState: {
                    nick: data.concern_obj.name,
                    dealer_price: data.concern_obj.dealer_price,
                    series_clue_count: data.concern_obj.series_clue_count
                },
                sameLevelCars: data.recommended_compare_series_list
            }
            console.log('carDetail');
        })
        return data
    }
     // done
    async _carDetail(ctx) {
        await axios.get(`${http}/motor/car_page/m/v1/series_all_json/?series_id=${ctx.request.url.split('=')[1]}`).then(_res => {
            _res = _res.data.data
            data = {
                online: _res.online.filter((p) => p.info.price ? p : null),
                offline:_res.offline.filter((p) => p.info.price ? p : null),
                series_id: _res.series_id,
                series_name: _res.series_name
            }
            console.log('_carDetail');
        })
        return data
    }
    // todo
    async _carCity(ctx) {
        const CarsCity = ctx.req.query.city
        await axios.get(`${http}/motor/dealer/m/v2/get_dealers_with_series/?series_id=4787&sort_type=3&city_name=${encodeURI(CarsCity)}`).then(_res => {
            data = _res
            console.log('_carCity');
        })
        return data.data
    }
    // todo
    async carRank(ctx) {
        await axios.get(`${http}/motor/car_show/v1/get_rank/?rank_type=${ctx.req.query.type}&offset=${ctx.req.query.nums}&limit=15`).then(_res => {
            data = _res
            console.log('carRank');
        })
        return data.data.data
    }
    // todo
    async usersDynamic(ctx) {
        await axios.get(`${http}/motor/stream_entrance/get_feed/v47/?aid=1230&category=motor_car_cmg&web_id=6989564955696055838`).then(_res => {
            console.log('usersDynamic');
            data = _res.data
            try {
                data = data.data.map((p) => {
                    return {
                        find_id: p.unique_id_str,
                        info: {
                            avatar_url: p.info?.user_info.avatar_url ? p.info.user_info.avatar_url : null,
                            name: p.info?.user_info.name ? p.info.user_info.name : null,
                            read_count: p.info.read_count,
                            image_list: p.info?.image_list ? p.info.image_list : null,
                            content: p.info?.content ? p.info.content : null,
                            circle_name: p.info?.auto_label ? p.info.auto_label.name : null
                        }
                    }
                })
            } catch (err) {

            }
        })
        return data
    }
    // todo
    async getJuheWeather(ctx) {
        await axios.get(`http://apis.juhe.cn/simpleWeather/query?city=${encodeURI(ctx.req.body.site)}&key=1b063d21bb1070bdb10a1d43c3dde4e3`).then(res => {
            console.log('getJuheWeather');
            data = res.data
        })
        return data
    }
    // todo
    async twoHander(ctx) {
        await axios.get(`${http}/motor/sh_go/api/buy_car/tab_sh_page`).then(_res => {
            console.log('twoHander');
            data = _res.data
        })
        return data
    }
    // todo
    async fourS(ctx) {
        await axios.get(`${http}/motor/sh_search/api/home/card_list?limit=10&city_name=%E5%8D%97%E6%98%8C&recall_type=certifys&offset=0&business_type=`).then(res => {
            console.log('fourS');
            data = res.data
        })
        return data
    }
    // todo
    async twoRecommend(ctx) {
        await axios.get(`${http}/motor/stream_entrance/get_feed/v47/?&category=sku_second_hand_motor_car&city_name=%E5%8D%97%E6%98%8C`).then(res => {
            console.log('twoRecommend');
            data = res.data.data
            // .map((p) => {
            //     return {
            //         find_id: p.unique_id_str,
            //         info: p.info
            //     }
            // })
        })
        return data
    }
    // todo
    async luxuryCar(ctx) {
        await axios.get(`${http}/motor/sh_search/api/home/card_list?limit=10&city_name=%E5%8D%97%E6%98%8C&recall_type=luxury_car&offset=0&business_type=`).then(res => {
            console.log('luxuryCar');
            data = res.data.data
        })
        return data
    }
}

module.exports = new dataController();